package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author ：seanyang
 * @date ：Created in 2019/7/2
 * @description ：业务报表实现类
 * @version: 1.0
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
	@Autowired
	private MemberDao memberDao;

	@Autowired
	private OrderDao orderDao;
	/**
	 * reportDate:null,
	 * todayNewMember :0,
	 * totalMember :0,
	 * thisWeekNewMember :0,
	 * thisMonthNewMember :0,
	 * todayOrderNumber :0,
	 * todayVisitsNumber :0,
	 * thisWeekOrderNumber :0,
	 * thisWeekVisitsNumber :0,
	 * thisMonthOrderNumber :0,
	 * thisMonthVisitsNumber :0,
	 * hotSetmeal:[]
	 * @return
	 */
	@Override
	public Map<String, Object> getBusinessReportData() {
		Map<String,Object> resultMap = new HashMap<>();
		try{
			// 获取今日
			String today = DateUtils.parseDate2String(DateUtils.getToday());
			// 获取本周第1天
			String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
			// 获取本月第1天
			String thisFirstDayInMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
			// 报表日期
			resultMap.put("reportDate",today);
			// 获取会员相关数据
			// * todayNewMember :0, totalMember :0, thisWeekNewMember :0, thisMonthNewMember :0,
			resultMap.put("todayNewMember",memberDao.findMemberCountByDate(today));
			resultMap.put("totalMember",memberDao.findMemberTotalCount());
			resultMap.put("thisWeekNewMember",memberDao.findMemberCountAfterDate(thisWeekMonday));
			resultMap.put("thisMonthNewMember",memberDao.findMemberCountAfterDate(thisFirstDayInMonth));
			/**
			 *  todayOrderNumber :0,
			 *  todayVisitsNumber :0,
			 *  thisWeekOrderNumber :0,
			 *  thisWeekVisitsNumber :0,
			 *  thisMonthOrderNumber :0,
			 *  thisMonthVisitsNumber :0,
			 */
			// 预约相关数据
			resultMap.put("todayOrderNumber",orderDao.findOrderCountByDate(today));
			resultMap.put("thisWeekOrderNumber",orderDao.findOrderCountByAfterDate(thisWeekMonday));
			resultMap.put("thisMonthOrderNumber",orderDao.findOrderCountByAfterDate(thisFirstDayInMonth));
			// 到诊相关数据
			resultMap.put("todayVisitsNumber",orderDao.findVisitsCountByDate(today));
			resultMap.put("thisWeekVisitsNumber",orderDao.findVisitsCountByAfterDate(thisWeekMonday));
			resultMap.put("thisMonthVisitsNumber",orderDao.findVisitsCountByAfterDate(thisFirstDayInMonth));
			// 获取热门数据 hotSetmeal:[]
			List<Map<String,Object>> list = orderDao.findHotSetmeal();
			for(Map<String,Object> one:list){
					Object value = one.get("setmeal_count");
					log.debug(">>>>obj:{} type:{}",value,value.getClass());

			}
			resultMap.put("hotSetmeal",list);
		}catch(Exception e){
		    e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 获取性别和对应数量，年龄段和对应数量
	 * @return
	 */
    @Override
    public Map<String,Object> findMemberCountBySex() {
		Map<String,Object> map=new HashMap();

		List<Map<String, Object>> list = memberDao.findByGender();
		List<Map<String, Object>> sexList = new ArrayList<>();
		for (Map<String, Object> sexMap : list) {
			//获取Map集合中name的值（1和2）

			//判断性别，获取数据后重新封装到新的map集合中
			divideByGender(sexList, sexMap, "1","男");

			divideByGender(sexList, sexMap, "2","女");
		}
		map.put("sex",sexList);

		//把年龄段的划分存入list集合
		ArrayList<String> arrayList = new ArrayList<>();
		arrayList.add("0-18岁");
		arrayList.add("18-30岁");
		arrayList.add("30-45岁");
		arrayList.add("45岁以上");
		//获取今年的年份
		int thisYear = Calendar.getInstance().get(Calendar.YEAR);
		List<Integer> yearList = memberDao.findByIdcard();
		int count4AgeBracketsOne=0;
		int count4AgeBracketsTwo=0;
		int count4AgeBracketsThree=0;
		int count4AgeBracketsFour=0;

		//遍历集合，获取身份证中的年份
		for (Integer idCardYear : yearList) {
			//计算身份证和今年相差几年
			int diffYear=thisYear - idCardYear;
			Map<String,Object> map3 =new HashMap<>();
			if (diffYear>=0 && diffYear<18){
				count4AgeBracketsOne++;
			}else if (diffYear>=18 && diffYear<30){
				count4AgeBracketsTwo++;
			}else if (diffYear>=30 && diffYear<45){
				count4AgeBracketsThree++;
			}else if (diffYear>=45){
				count4AgeBracketsFour++;
			}else {
				new RuntimeException("年龄超出范围");
			}
		}
		List<Map<String, Object>> ageList = new ArrayList<>();

		creatMemberAgeBracketsCount(arrayList.get(0), count4AgeBracketsOne, ageList);
		creatMemberAgeBracketsCount(arrayList.get(1), count4AgeBracketsTwo, ageList);
		creatMemberAgeBracketsCount(arrayList.get(2), count4AgeBracketsThree, ageList);
		creatMemberAgeBracketsCount(arrayList.get(3), count4AgeBracketsFour, ageList);


		map.put("age",ageList);
		return map;



    }

	/**
	 * 按年龄段获取数据，存入map中
	 * @param ageBracket 年龄段
	 * @param count  每个年龄段对应的数量
	 * @param mapList
	 */
	private void creatMemberAgeBracketsCount(String ageBracket, int count, List<Map<String, Object>> mapList) {
		Map<String, Object> map= new HashMap<>();
		map.put("name",ageBracket);
		map.put("value",count);
		mapList.add(map);
	}


	/**
	 *
	 * @param list
	 * @param sexMap
	 * @param ageTag  在数据库中性别的标识（1,2）
	 * @param gender  性别
	 */
	private void divideByGender(List<Map<String, Object>> list, Map<String, Object> sexMap,  String ageTag,String gender ) {
		if (ageTag.equals(sexMap.get("name").toString())) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", gender);
			map.put("value", sexMap.get("value"));
			list.add(map);
		}
	}
}
